package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "购物车相关功能")
@RequestMapping("/user/shoppingCart")
public class ShoppingCartController {


    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     *  添加购物车
      * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result<String> addCart(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("【购物车 - 添加购物车】 dto:{}",shoppingCartDTO);
        Integer tag = shoppingCartService.addCart(shoppingCartDTO);
        return Result.success(tag > 0 ? "新增成功":"新增失败");
    }

    /**
     * 购物车显示
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("购物车显示")
    public Result<List<ShoppingCart>> findAll() {
        log.info("【购物车-显示数据】");
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll();
        return Result.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean() {
        log.info("【清空购物车】");
        Integer tag = shoppingCartService.clean();
        if (tag > 0) {
            return Result.success("清空成功");
        }else  {
            return Result.error("清空失败");
        }
    }

}
